package javaMSCRM;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.JTableHeader;

import org.apache.log4j.Logger;

import com.microsoft.schemas.crm._2007.coretypes.CrmAuthenticationToken;
import com.microsoft.schemas.crm._2007.webservices.CrmAuthenticationTokenDocument;
import com.microsoft.schemas.crm._2007.webservices.CrmServiceStub;
import com.microsoft.schemas.crm._2007.webservices.EntityName;

/**
 * Java MSCRM main class.
 * @author Zoran IVANOV
 * @version     0.1
 */
public class JavaMSCRM  extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1L;

	String UserId = null;
	int recordsPerPage = 50;
	ArrayList<String> EntriesIDList = new ArrayList<String>();
	String Entity = null;
	
    //Crm Service
    CrmServiceStub stub = null;
    CrmAuthenticationTokenDocument catd = null;  
    CrmAuthenticationToken token = null;
    
	//Controls Definitions
    JPanel 	HeaderBarContainer,HeaderBarContainerTop,HeaderBarContainerMiddle,
		    HeaderBarContainerBottom,SearchBarContainer,SearchContainer,
		    ViewFilterContainer,NewDeleteJP,LettersContainer,PagingContainer;
    JLabel HeaderTitle,ViewFilterJL,PageNumber,MainMessage;
    JButton HeaderUserFullName,SearchButton,NewEntryJB,DeleteEntryJB, NextPage, PreviousPage,
    		ButtonAll,ButtonA,ButtonB,ButtonC,ButtonD,ButtonE,ButtonF,ButtonG,ButtonH,ButtonI,
    		ButtonJ,ButtonK,ButtonL,ButtonM,ButtonN,ButtonO,ButtonP,ButtonQ,ButtonR,ButtonS,
    		ButtonT,ButtonU,ButtonV,ButtonW,ButtonX,ButtonY,ButtonZ;
    JTextField SearchField = new JTextField(20);
    JComboBox ViewFilterJCB = new JComboBox();

    //Table
    
    TableModel model = new TableModel();   
    JTable table = new JTable(model);
    JScrollPane TableScrollPane = new JScrollPane(table); 
    JTableHeader TableHeader = table.getTableHeader();
    
    static Logger log = Logger.getLogger("JavaMSCRM");
    
    /**
     * Java MSCRM Constructor
     */
	public JavaMSCRM() {		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    setSize(800,600);
		setTitle("Java MSCRM");
		Image img = Toolkit.getDefaultToolkit().getImage("C:/Users/Zoran/workspace/JavaMSCRM/src/icon.png");
		setIconImage(img);		
		//Header
		add(new Header(this), BorderLayout.PAGE_START); 
	    //Menu
		setJMenuBar(new Menu(this));		
		//Table
	    table.setShowVerticalLines(false);

	    //Footer
	    add(new StatusBar(this), BorderLayout.PAGE_END);
		setVisible(false);				
		//Launch Login window
		new Login(this);	
	}
	
	 /**
     * Clear the JavaMSCRM records table.
     */
	public void clearTable(){
		remove(table);
        remove(TableScrollPane);
        model = new TableModel();
        table = new JTable(model);
	    TableScrollPane = new JScrollPane(table);
	    ((JComponent)getContentPane()).revalidate();
	}
	
	/**
	* Action listener.
	*/
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource () == HeaderUserFullName){
   			new User(this, UserId);
        }else if(e.getSource () == NewEntryJB){
        	if(Entity == EntityName.ACTIVITYPOINTER.toString()){
        		new JavaMSCRMActivity(this);
        	}
        	if(Entity == EntityName.ACCOUNT.toString()){
        		new JavaMSCRMAccount(this);
        	}
        	if(Entity == EntityName.CONTACT.toString()){
        		new JavaMSCRMContact(this);
        	}
        	if(Entity == EntityName.LEAD.toString()){
        		new JavaMSCRMLead(this);
        	}
        	if(Entity == EntityName.OPPORTUNITY.toString()){
        		new JavaMSCRMOpportunity(this);
        	}
        }else if(e.getSource () == DeleteEntryJB){
        	new DeleteEntities(this);
        }
	}

	/**
	* Main method.
	* @param args
	*/
	public static void main(String[] args) {
		new JavaMSCRM();
	}
}
